package com.ldj.hibernate;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;

import com.ldj.struts.form.VisiteInfoForm;

public class VisiteInfoDAO extends BaseHibernateDAO {
	public VisiteInfoDAO(HttpServletRequest request, HttpServletResponse response) {
		super(request, response);
		// TODO Auto-generated constructor stub
	}

	private static final Log log = LogFactory.getLog(VisiteInfoDAO.class);
	
	public List findAllPage(String userId, String empInfoName, VisiteInfoForm pForm) {
		try {
			String queryString = "from VisiteInfo where petitionInfoDelOper='0'";
			if(userId != null && !"".equals(userId)){
				queryString += " and petitionInfoCreator ='" + userId + "'";
			}
			if(pForm.getPetitionInfoMode() != null && !"".equals(pForm.getPetitionInfoMode())){
				queryString += " and petitionInfoMode='" + pForm.getPetitionInfoMode() + "'";
			}
			if(empInfoName != null && !"".equals(empInfoName)){
				queryString += " and employerInfo.empInfoName like '%" + empInfoName + "%'";
			}
			if(pForm.getPetitionInfoSource() != null && !"".equals(pForm.getPetitionInfoSource())){
				queryString += " and petitionInfoSource = '" + pForm.getPetitionInfoSource() + "'";
			}
			if(pForm.getPetitionInfoType() != null && !"".equals(pForm.getPetitionInfoType())){
				queryString += " and petitionInfoType = '" + pForm.getPetitionInfoType()+ "'";
			}
			queryString += " order by petitionInfoRegister desc";
			Query queryObject = getSession().createQuery(queryString);
			this.setPageSize(9);
			return this.jlist(queryObject);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	
	public List findVisiteInfoAllPage(String empInfoName, VisiteInfoForm pForm) {
		try {
			String queryString = "from VisiteInfo where petitionInfoDelOper='0'";
			if(pForm.getPetitionInfoMode() != null && !"".equals(pForm.getPetitionInfoMode())){
				queryString += " and petitionInfoMode='" + pForm.getPetitionInfoMode() + "'";
			}
			if(empInfoName != null && !"".equals(empInfoName)){
				queryString += " and employerInfo.empInfoName like '%" + empInfoName + "%'";
			}
			if(pForm.getPetitionInfoSource() != null && !"".equals(pForm.getPetitionInfoSource())){
				queryString += " and petitionInfoSource = '" + pForm.getPetitionInfoSource() + "'";
			}
			if(pForm.getPetitionInfoType() != null && !"".equals(pForm.getPetitionInfoType())){
				queryString += " and petitionInfoType = '" + pForm.getPetitionInfoType()+ "'";
			}
			queryString += " order by petitionInfoRegister desc";
			Query queryObject = getSession().createQuery(queryString);
			this.setPageSize(9);
			return this.jlist(queryObject);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public VisiteInfo findById(java.lang.String id) {
		log.debug("getting VisiteInfo instance with id: " + id);
		try {
			VisiteInfo instance = (VisiteInfo) getSession().get(
					"com.ldj.hibernate.VisiteInfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public void save(VisiteInfo transientInstance) {
		log.debug("saving VisiteInfo instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	
	public VisiteInfo merge(VisiteInfo detachedInstance) {
		try {
			VisiteInfo result = (VisiteInfo) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}
	
	public List findFromIdArray(String[] id){
		try {
			if(id != null && id.length > 0){
				String idChar = "";
				for(int i = 0; i < id.length; i ++){
					idChar += id[i] + ",";
				}
				idChar = idChar.substring(0, idChar.length() - 1);
				idChar = idChar.replace(",", "','");
				String queryString = "from VisiteInfo where petitionInfoDelOper='0' and petitionInfoId in ('" + idChar + "')";
				Query queryObject = getSession().createQuery(queryString);
				return queryObject.list();
			}
			return null;
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	
	
	public void delete(VisiteInfo persistentInstance) {
		log.debug("deleting VisiteInfo instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
	
	public int findMaxCode(String year){
		try {
			String queryString = "select max(substr(petitionInfoRegister,9)) as t from VisiteInfo where petitionInfoRegister like '" + year + "%'";
			Query queryObject = getSession().createQuery(queryString);
			List list = queryObject.list();
			if(list != null && list.size() > 0){
				String code = (String)list.get(0);
				if(code != null && !"".equals(code))
					return Integer.parseInt(code);
			}
			return 0;
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
}